/*
 	Software Author: Mike Moore
 	Vectrics Flashrecruit
 	Open source job board and applicant tracking system.  Built with enterprise Java.
 */
package org.vectrics.skill.struts.pagelist;

import java.util.Iterator;
import java.util.List;
import org.apache.ecs.html.Input;
import org.apache.log4j.Logger;
import org.vectrics.common.html.BorderTable;
import org.vectrics.common.html.Cell;
import org.vectrics.common.html.Form;
import org.vectrics.common.html.Row;
import org.vectrics.common.html.Table;
import org.vectrics.common.html.TextHelper;
import org.vectrics.common.util.StringUtils;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.struts.ButtonUtils;
import org.vectrics.recruiter.web.tags.SkillGui;
import org.vectrics.skill.Skill;
import org.vectrics.skill.SkillRating;
import org.vectrics.skill.SkillScoringMethod;
import org.vectrics.skill.UserEvaluation;
import org.vectrics.struts.PageListTag;

/**
 * @author MMoore
 */
public abstract class SkillPageListTag extends PageListTag {
	private Logger log = Logger.getLogger(this.getClass());
	
	/**
	 * 
	 */
	public SkillPageListTag() {
		super();
	}

	abstract protected boolean getAllowRatingEntry();
	abstract protected boolean getMakeCheckBoxes();
	abstract protected SkillRating getSkillRating(Skill skill);
	abstract protected String getFinishLink();
	
	protected String getDescription() {
		return null;
	}
	

	@Override
	protected String getPageListLink() {
		return "skillsList.do";
	}

	@Override
	protected void onFormCreate(Form form) {
		Long evaluationId = this.getRequest().getLongParameter("evaluationId");
		form.addHidden("evaluationId", evaluationId.toString());
		UserEvaluation evaluation = RecruitServiceLocator.getSkillProvider().findEvaluationById(evaluationId);
		form.addHidden("userId", evaluation.getPerson().getId().toString());
	}

	protected void showList(String description, Cell mainCell,
			List keyList, Form form) {
		log.info("showList() called - renders the HTML for the page list");
		BorderTable table = new BorderTable();
		Row headerRow = table.addRow();
        headerRow.setDefaultBgImage(this.getContextPath() + "/site-graphics/tbl_hdr.gif");
		if (this.getMakeCheckBoxes()) {
			headerRow.addHeaderCell(15);
		}
		Cell descHeaderCell = headerRow.addHeaderCell(280);
		descHeaderCell.addText("tableHdrFont", this.getPhrase("phrase.skill"));

		if (this.getAllowRatingEntry()) {
			Cell hdrCell = headerRow.addHeaderCell(120);
			hdrCell.addText("tableHdrFont", this.getPhrase("phrase.skill.rating"));
		}
		
		Iterator iter = keyList.iterator();
		while (iter.hasNext()) {
			Skill skill = (Skill)iter.next();
			log.info("Displaying skill: " + skill);
			Row row = table.addRow();
			if (this.getMakeCheckBoxes()) {
				Cell checkCell = row.addCell();
				Input checkInput = new Input();
				checkInput.setType(Input.checkbox);
				checkInput.setName("checked_" + skill.getCode());
				checkInput.setClass("formCheckBoxClass");
				checkCell.addElement(checkInput);
				
				Input hidden = new Input();
				hidden.setType(Input.hidden);
				hidden.setName("skill_" + skill.getCode());
				hidden.setValue("true");
				form.addElement(hidden);
			}
			Cell descCell = row.addCell();
			String skillDescription = skill.getDescription();
			if (StringUtils.isNullOrBlank(skillDescription)) {
				skillDescription = "&nbsp;";
			}
			descCell.addElement(TextHelper.getFont("subFont", skill.getDescription()));
			SkillRating skillRating = this.getSkillRating(skill);
			if (this.getAllowRatingEntry()) {
				Cell ratingCell = row.addCell();
				String scoreCode = skill.getScoringMethod().getCode(); 
				if (scoreCode.equals(SkillScoringMethod.SCORING_METHOD_NONE)) {
					ratingCell.addElement("&nbsp;");
				} else if (scoreCode.equals(SkillScoringMethod.SCORING_METHOD_YES_NO)) {
					SkillGui.displayYesNo(skillRating, skill, ratingCell);
				} else if (scoreCode.equals(SkillScoringMethod.SCORING_METHOD_LEVEL)) {
				    Table ratingTable = SkillGui.makeSkillLevelTable(skillRating, skill);
					ratingCell.addElement(ratingTable);
				} 
			}
		}

		mainCell.addElement(table);
		
		String finishHref = getFinishLink();
		if (finishHref != null) {
			if (form != null) {
				log.info("Adding 'finished' button to form");
				form.addElement(
					ButtonUtils.makeButtonInput(this.getRequest(), "button.finished", this.getLocale(), "finishedButton", "finishedButton")); 
			} else {
				log.info("Form is null, not adding finish button");
			}
		}
	}


}
